package com.huike.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.dto.CourseDto;
import com.huike.entity.Course;
import com.huike.entity.SysDictData;
import com.huike.mapper.CourseMapper;
import com.huike.mapper.SysDictDataMapper;
import com.huike.result.PageResult;
import com.huike.result.Result;
import com.huike.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;
    @Override
    public Result<PageResult> queryPage(CourseDto courseDto) {

        if (courseDto.getPage() == null || courseDto.getPageSize() == null) {
            courseDto.setPage(1);
            courseDto.setPageSize(10);
        }
        IPage<Course> iPage = new Page<>(courseDto.getPage(), courseDto.getPageSize());

        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        if (courseDto.getCourseNumber() != null) {
            queryWrapper.lambda().like(Course::getCourseNumber, courseDto.getCourseNumber());
        }
        if (courseDto.getSubject() != null) {
            queryWrapper.lambda().like(Course::getSubject, courseDto.getSubject());
        }
        if (courseDto.getApplicablePeople() != null) {
            queryWrapper.lambda().eq(Course::getApplicablePeople, courseDto.getApplicablePeople());

            // applicablePeople，字典
        }
        if (courseDto.getCreateTime() != null) {

            LocalDateTime localDateTime = courseDto.getCreateTime().toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDateTime();

            LocalDateTime startTime = LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MAX);

            queryWrapper.lambda().between(Course::getCreateTime, startTime,endTime);
        }
        if (courseDto.getCourse() != null) {
            queryWrapper.lambda().eq(Course::getCourse, courseDto.getCourse());
            // subject,字典
        }

        queryWrapper.lambda().eq(Course::getIsDelete, 0);
        IPage<Course> page = courseMapper.selectPage(iPage, queryWrapper);

        PageResult<Course> pageResult = new PageResult<>(page.getTotal(), page.getRecords());

        return Result.success(pageResult);
    }

    @Override
    public Result updateCourse(Course course) {

        courseMapper.updateById(course);
        return new Result();
    }

    @Override
    public Result deleteList(List<Integer> ids) {

        for (Integer id : ids) {
            Course course = courseMapper.selectById(id);
            course.setIsDelete(1);
            courseMapper.updateById(course);
        }
        return new Result<>();
    }

    @Override
    public Result findById(Integer id) {

        Course course = courseMapper.selectById(id);
        return Result.success(course);
    }

    @Override
    public Result addCourse(Course course) {
        course.setCreateTime(LocalDateTime.now());
        courseMapper.insert(course);
        return Result.success();
    }

    @Override
    public Result subjectList() {

        QueryWrapper<SysDictData> dictDataQueryWrapper = new QueryWrapper<>();
        dictDataQueryWrapper.lambda().eq(SysDictData::getDictType,"course_subject");
        List<SysDictData> subjectList = sysDictDataMapper.selectList(dictDataQueryWrapper);
        return Result.success(subjectList);

    }

    @Override
    public Result applicablePeopleList() {
        QueryWrapper<SysDictData> dictDataQueryWrapper = new QueryWrapper<>();
        dictDataQueryWrapper.lambda().eq(SysDictData::getDictType,"applicable_person");
        List<SysDictData> applicablePeopleList = sysDictDataMapper.selectList(dictDataQueryWrapper);
        return Result.success(applicablePeopleList);
    }

    @Override
    public Result courseList() {


        List<Course> courses = courseMapper.selectList(null);

        return Result.success(courses);
    }

}
